let deviceName = sessionStorage.getItem("selectDeviceName");
let alarmRuleList = [];

$(function () {
    $('.page-header').html(deviceName + '告警规则列表');
})

function loadAlarmRuleData() {
    // 加载告警规则信息
    showLoading();

    $.post(serverUrl + ':' + serverPort + serverName + '/alarmRule/page', {
        deviceCode: sessionStorage.getItem("selectDeviceCode"), pageNum: 1, pageSize: 1000
    }, function (result) {
        hideLoading();

        let obj = JSON.parse(result);

        alarmRuleList = obj.data.content;

        renderData(obj.data.content);
    });
}

// 渲染底部显示数据
function renderData(dataList) {
    let content = '';
    let typeName = '';

    $.each(dataList, function (i, item) {
        switch (item.type) {
            case 2011:
                // 实时数据
                typeName = '实时值';
                break;
            case 2051:
                // 分钟数据
                typeName = '分钟值';
                break;
            case 2061:
                // 小时数据
                typeName = '小时值';
                break;
            case 2031:
                // 日均数据
                typeName = '日均值';
                break;
        }

        content += '<div class="card" onclick="toAlarmRuleInfoPage(' + item.id + ')">';
        content += '<div class="card-content">';

        let tempNum;

        if (Number(item.blueNum) > 0) {
            tempNum = item.blueNum;
        } else if (Number(item.yellowNum) > 0) {
            tempNum = item.yellowNum;
        } else {
            tempNum = item.redNum;
        }

        switch (item.dataType) {
            case 0:
                // 小于预警
                content += '<div class="card-title">' + item.codeDictName + typeName + '低于' + tempNum + item.codeDictUnit + '预警</div>';
                break;
            case 1:
                // 大于预警
                content += '<div class="card-title">' + item.codeDictName + typeName + '超过' + tempNum + item.codeDictUnit + '预警</div>';
                break;
            case 2:
                // 等于预警
                content += '<div class="card-title">' + item.codeDictName + typeName + '等于' + tempNum + item.codeDictUnit + '预警</div>';
                break;
            case 3:
                // 固定预警
                content += '<div class="card-title">' + item.codeDictName + typeName + '固定值预警</div>';
                break;
        }

        content += '<div class="switch-group">';
        content += '<div class="switch-item">';
        content += '<span class="switch-label">排放标准：' + item.redNum + " " + item.codeDictUnit + '</span>';
        content += '</div>';
        content += '<div class="switch-item">';

        if (null !== item.lastTime) {
            content += '<span class="switch-label">触发检验时间：' + formatTime(item.lastTime) + '</span>';
        } else {
            content += '<span class="switch-label">触发检验时间：暂无</span>';
        }

        content += '</div></div></div></div>';
    });

    $('.mainContent').html(content);

    let addBtnDiv = $('#addDiv').clone();
    addBtnDiv.css("display", '');

    $('.mainContent').append(addBtnDiv);
}

function toAlarmRuleInfoPage(alarmRuleId) {
    if (null !== alarmRuleId) {
        $.each(alarmRuleList, function (i, item) {
            if (item.id === alarmRuleId) {
                // 跳转到告警规则详情管理界面
                sessionStorage.setItem('selectAlarmRuleId', item.id);
                sessionStorage.setItem('selectAlarmRuleDataType', item.dataType);
                sessionStorage.setItem('selectAlarmRuleDeviceCode', item.device.code);
                sessionStorage.setItem('selectAlarmRuleType', item.type);
                sessionStorage.setItem('selectAlarmRuleCodeDictName', item.codeDictName);
                sessionStorage.setItem('selectAlarmRuleCodeDictUnit', item.codeDictUnit);
                sessionStorage.setItem('selectAlarmRuleBlueNum', item.blueNum);
                sessionStorage.setItem('selectAlarmRuleYellowNum', item.yellowNum);
                sessionStorage.setItem('selectAlarmRuleRedNum', item.redNum);
                sessionStorage.setItem('selectAlarmRuleAlarmType', item.alarmType);
                sessionStorage.setItem('selectAlarmRuleAlarmTime', item.alarmTime);
            }
        });
    } else {
        // 跳转到告警规则详情管理界面
        sessionStorage.setItem('selectAlarmRuleId', '');
        sessionStorage.setItem('selectAlarmRuleDataType', 0);
        sessionStorage.setItem('selectAlarmRuleDeviceCode', '');
        sessionStorage.setItem('selectAlarmRuleType', 2011);
        sessionStorage.setItem('selectAlarmRuleCodeDictName', '');
        sessionStorage.setItem('selectAlarmRuleCodeDictUnit', '');
        sessionStorage.setItem('selectAlarmRuleBlueNum', '');
        sessionStorage.setItem('selectAlarmRuleYellowNum', '');
        sessionStorage.setItem('selectAlarmRuleRedNum', '');
        sessionStorage.setItem('selectAlarmRuleAlarmType', 1);
        sessionStorage.setItem('selectAlarmRuleAlarmTime', '');
    }

    window.location.href = 'wx/center/alarmRuleInfo';
}